<?php

namespace app\admin\controller;

use think\facade\Session;
use app\admin\controller\Base;
use app\admin\model\AuthRule as RuleModel;
use app\admin\model\AuthGroup as GroupModel;

class AuthGroup extends Base
{
    public function initialize()
    {
        parent::initialize();
        $this->Model = new GroupModel();
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        if($this->request->isPost()){
            $data = $this->getPostData();
            $res = $this->Model->createOrUpdate($data);
            if($res["status"]){
                Session::flash('success', $res['msg']);
                $this->success($res['msg'], 'index');
            }else{
                Session::flash('danger', $res['msg']);
                $this->error($res['msg']);
            }
        }else{
            $MenuModel = new RuleModel();
            $all_menu_tree = $MenuModel->findWithDescendants(0, true);
            $this->assign("group_rules_id", []);
            $this->assign("all_menu_tree", $all_menu_tree);
            $this->assign('group', ["status" => true]);
            return $this->fetch('form');
        }
    }

    /**
     * 保存更新的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function update($id = 0)
    {
        $id = intval($id);
        $group = $this->Model->where('id', $id)->find();
        if(empty($group)){
            Session::flash('danger', '未找到编辑记录');
            $this->redirect('index');
        }else if($group['is_super']){
            Session::flash('danger', '超级管理员级不能编辑');
            $this->redirect('index');
        }

        if($this->request->isPost()){
            $data = $this->getPostData();
            $res = $this->Model->createOrUpdate($data, $id);
            if($res['status']){
                Session::flash('success', $res['msg']);
                $this->success($res['msg'], 'index');
            }else{
                Session::flash('danger', $res['msg']);
                $this->error($res['msg']);
            }
        }else{
            $MenuModel = new RuleModel();
            $all_menu_tree = $MenuModel->findWithDescendants(0, true);
            $group_rules_id = [];
            if(isset($group["rules"])){
              $group_rules_id = explode(",", $group["rules"]);
            }
            $this->assign("group_rules_id", $group_rules_id);
            $this->assign("all_menu_tree", $all_menu_tree);
            $this->assign('group', $group);
            return $this->fetch('form');
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id = 0)
    {
        if($this->request->isDelete()){
            $res = $this->Model->deleteItem($id);
            if($res['status']){
                Session::flash('success', $res['msg']);
                $this->success($res['msg'], url('index'));
            }else{
                Session::flash('danger', $res['msg']);
                $this->error($res['msg'], url('index'));
            }
        }else{
            $this->error('您请求的方法不存在', url('index'));
        }
    }

    private function getPostData()
    {
        $fields = [
            ['name' => 'title', 'type' => 'string', 'default' => ''],
            // 使用switch开关，所以默认值必须是false，因为switch为关闭状态时返回的值里没有该字段
            ['name' => 'status', 'type' => 'boolean', 'default' => false],
            ['name' => 'rules', 'type' => 'arr2str', 'default' => ''],
        ];

        return $this->parsePostParam($fields, []);
    }
}
